package com.nf.health.app.inquiry.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.db.DbManager;
import com.nf.health.app.inquiry.bean.PatientUser;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DialogHelp;
import com.nf.health.app.utils.ViewUtil;

import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.qqtheme.framework.picker.DatePicker;

/**
 * 添加就诊人
 */
public class AddPatientActivity extends BaseActivity {


    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.ll_name)
    LinearLayout llName;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.ll_sex)
    LinearLayout llSex;
    @Bind(R.id.tv_birthday)
    TextView tvBirthday;
    @Bind(R.id.ll_birthday)
    LinearLayout llBirthday;
    @Bind(R.id.btn_commit)
    Button btnCommit;
    @Bind(R.id.tv_name_hint)
    TextView tvNameHint;
    @Bind(R.id.tv_sex_hint)
    TextView tvSexHint;
    @Bind(R.id.tv_birthday_hint)
    TextView tvBirthdayHint;
    private int gender = 1;//1代表男

    AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_add_patient);
        ButterKnife.bind(this);

        ViewUtil.setText(this, R.id.titlebar_title_tv, "个人信息");
        initListener();
    }

    private void initListener() {
        llBirthday.setOnClickListener(this);
        llSex.setOnClickListener(this);
        llName.setOnClickListener(this);
        btnCommit.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_name:
                ActivityUtils.showActivityForResult(this, AddPatientNameActivity.class, 0);
                break;
            case R.id.ll_sex:
                AlertDialog.Builder builder = DialogHelp.getDialog(this);
                builder.setTitle("选择性别");
                View view = LayoutInflater.from(this).inflate(R.layout.dialog_chose_sex, null);
                view.findViewById(R.id.tv_male).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog != null) {
                            gender = 1;
                            dialog.dismiss();
                            tvSex.setText("男");
                            tvSexHint.setVisibility(View.INVISIBLE);
                        }
                    }
                });
                view.findViewById(R.id.tv_female).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog != null) {
                            gender = 2;
                            dialog.dismiss();
                            tvSex.setText("女");
                            tvSexHint.setVisibility(View.INVISIBLE);
                        }
                    }
                });
                builder.setView(view);
                    builder.setNegativeButton("取消", null);
                //一定要用show返回的dialog,否则不能保证显示的就是此dialog
                dialog = builder.show();
                break;
            case R.id.ll_birthday:
                DatePicker picker = new DatePicker(this);
                picker.setRange(1900, Calendar.getInstance().get(Calendar.YEAR));
                picker.setOffset(1);
                picker.setSubmitTextColor(getResources().getColor(R.color.theme_color));
                final int currentYear = Calendar.getInstance().get(Calendar.YEAR);
                final int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
                final int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
                picker.setSelectedItem(currentYear, currentMonth, currentDay);
                picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
                    @Override
                    public void onDatePicked(String year, String month, String day) {
                        if(Integer.parseInt(year) >= currentYear){
                            if(Integer.parseInt(month) > currentMonth){
                                showToast("日期选择错误");
                            }else{
                                if(Integer.parseInt(month) == currentMonth){
                                    if(Integer.parseInt(day) > currentDay){
                                        showToast("日期选择错误");
                                    }else{
                                        tvBirthday.setText(year + "-" + month + "-" + day);
                                        tvBirthdayHint.setVisibility(View.INVISIBLE);
                                    }
                                }else {
                                    tvBirthday.setText(year + "-" + month + "-" + day);
                                    tvBirthdayHint.setVisibility(View.INVISIBLE);
                                }
                            }
                        }else {
                            tvBirthday.setText(year + "-" + month + "-" + day);
                            tvBirthdayHint.setVisibility(View.INVISIBLE);
                        }
                    }
                });
                picker.show();
                break;
            case R.id.btn_commit:
                savePatient();
                break;
        }
    }

    private void savePatient() {
        String patientName = tvName.getText().toString().trim();
        String birthday = tvBirthday.getText().toString();
        if (TextUtils.isEmpty(patientName) || TextUtils.isEmpty(birthday)) {
            showToast("请输入姓名或生日");
            return;
        }
        PatientUser user = new PatientUser();
        user.setNick(patientName);
        user.setGender(gender);
        user.setBirthday(birthday);
        user.setChecked(1);
        //
        DbManager.getInstance().updatePatient();
        //保存就诊人信息
        long num = DbManager.getInstance().savePatient(user);
        if (num != -1) {
            setResult(RESULT_OK);
            finish();
            showToast("添加成功~");
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle b = data.getExtras(); //data为B中回传的Intent
            String name = b.getString("name");//str即为回传的值
            tvName.setText(name);
            tvNameHint.setVisibility(View.INVISIBLE);
        }
    }
}
